package com.atguigu.product.products.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.product.products.vo.respVo.OrderToProductQuerySpuInfoVo;
import com.atguigu.product.products.vo.respVo.SpuSaveVo.SpuSaveReqVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.product.products.entity.PmsSpuInfoEntity;
import com.atguigu.product.products.service.PmsSpuInfoService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * spu信息
 *
 * @author qhw
 * @email qhw@gmail.com
 * @date 2021-08-03 19:57:10
 */
@RestController
@RequestMapping("product/spuinfo")
public class PmsSpuInfoController {
    @Autowired
    private PmsSpuInfoService pmsSpuInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("products:pmsspuinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = pmsSpuInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("products:pmsspuinfo:info")
    public R info(@PathVariable("id") Long id) {
        PmsSpuInfoEntity pmsSpuInfo = pmsSpuInfoService.getById(id);

        return R.ok().put("pmsSpuInfo", pmsSpuInfo);
    }


    /**
     * 商品上架
     */
    @PostMapping("/{spuId}/up")
    public R up(@PathVariable("spuId") Long spuId) {
        pmsSpuInfoService.up(spuId);

        return R.ok();
    }

    /**
     * 保存商品
     */
    @RequestMapping("/save")
    public R save(@RequestBody SpuSaveReqVo spuSaveReqVo) {
        pmsSpuInfoService.saveDetail(spuSaveReqVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("products:pmsspuinfo:update")
    public R update(@RequestBody PmsSpuInfoEntity pmsSpuInfo) {
        pmsSpuInfoService.updateById(pmsSpuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("products:pmsspuinfo:delete")
    public R delete(@RequestBody Long[] ids) {
        pmsSpuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 列表
     */
    @GetMapping("/order/{skuId}")

    public R orderSpu(@PathVariable("skuId") Long skuId) {
        OrderToProductQuerySpuInfoVo orderToProductQuerySpuInfoVo = pmsSpuInfoService.orderSpu(skuId);

        return R.ok().put("order", orderToProductQuerySpuInfoVo);
    }


}
